package czh.cocoa.sample;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.location.GpsStatus.NmeaListener;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import czh.cocoa.animation.CAAnimation;
import czh.cocoa.animation.CAAnimationDelegate;
import czh.cocoa.animation.CAAnimationGroup;
import czh.cocoa.animation.CABasicAnimation;
import czh.cocoa.animation.CALayer;
import czh.cocoa.animation.CAMediaTimingFunction;
import czh.cocoa.animation.CATransform3D;
import czh.cocoa.animation.CATransformAction;
import czh.cocoa.foundation.NSArray;
import czh.cocoa.foundation.NSDictionary;
import czh.cocoa.foundation.NSIndexPath;
import czh.cocoa.foundation.NSMutableString;
import czh.cocoa.foundation.NSNumber;
import czh.cocoa.foundation.NSObject;
import czh.cocoa.foundation.NSSelector;
import czh.cocoa.foundation.NSString;
import czh.cocoa.graphics.CGAffineTransform;
import czh.cocoa.graphics.CGColor;
import czh.cocoa.graphics.CGContext;
import czh.cocoa.graphics.CGImage;
import czh.cocoa.graphics.CGPoint;
import czh.cocoa.graphics.CGRect;
import czh.cocoa.ui.UIAnimationBlock;
import czh.cocoa.ui.UIApplication;
import czh.cocoa.ui.UIApplicationDelegate;
import czh.cocoa.ui.UIColor;
import czh.cocoa.ui.UIFont;
import czh.cocoa.ui.UIImage;
import czh.cocoa.ui.UIKeyInput;
import czh.cocoa.ui.UINavigationController;
import czh.cocoa.ui.UIScreen;
import czh.cocoa.ui.UITabBarController;
import czh.cocoa.ui.UITargetAction;
import czh.cocoa.ui.UITextAlignment;
import czh.cocoa.ui.UITouch;
import czh.cocoa.ui.UIView;
import czh.cocoa.ui.UIViewController;
import czh.cocoa.ui.UIWindow;
import czh.cocoa.ui.gesture.UIPanGestureRecognizer;
import czh.cocoa.ui.gesture.UIPinchGestureRecognizer;
import czh.cocoa.ui.gesture.UITapGestureRecognizer;
import czh.cocoa.ui.impl.*;

import static czh.cocoa.ui.UIView.*;
import static czh.cocoa.ui.impl.UIControl.*;
import static czh.cocoa.ui.UIKit.*;
import static czh.cocoa.graphics.CGContext.*;

public class AppDelegate extends NSObject implements UIApplicationDelegate, 
							UIScrollViewDelegate, UIPickerViewDataSource, UIPickerViewDelegate {

	private MyView mView, mView2, mView3;
	
	private UIImageView mImageView;
	
	private UILabel mLabel;
	
	private UIButton mButton;
	
	private UIScrollView mScrollView;
	
	private MyTableViewController mTableCtrl;
	
	private UIWindow mWindow;
	
	private UITextField mTextField;
	
	private UIProgressView mProgressView;
	
	private UISwitch mSwitch;
	
	private UIActivityIndicatorView mLoadingView;
	
	private UIDatePicker mDatePicker;
	
	private UIViewController mRoot;
	
	private UINavigationController mNavi;
	
	private int mNumber = 1;
	
	private UIAlertView dialog;
	
	@Override
	public void applicationDidFinishLaunchingWithOptions(
			UIApplication application, NSDictionary launchOptions) {

		UIImage.imageNamed(NSTR("tabicon")); 
		
		CGRect bounds = UIScreen.mainScreen().applicationFrame();
		UIWindow window = new UIWindow(bounds);
		
		MyViewController ctrl = new MyViewController(1);
		MyViewController ctrl2 = new MyViewController(2);
		MyViewController ctrl3 = new MyViewController(3);
		MyViewController ctrl4 = new MyViewController(4);
		NSArray ctrls = NSArray.arrayWithObjects(ctrl,ctrl2,ctrl3,ctrl4);
		
		UITabBarController tab = new UITabBarController();
		tab.setViewControllers(ctrls);
		
		window.setRootViewController(tab);
		
		
//		UIImageView bgview = new UIImageView(CGRectMake(0, 0, 480, 200)); 
//		bgview.setImage(UIImage.imageNamed(NSTR("cocoa/tabbar"))); 
//		bgview.setBackgroundColor(UIColor.yellowColor());
//		window.addSubview(bgview);
	
		window.makeKeyAndVisible();
		
		mWindow = window;
		mRoot = tab;
	}
	
	public void test4() {
		UITextView textView = new UITextView(CGRectMake(20, 100, 320, 300)); 
		textView.setBackgroundColor(UIColor.orangeColor());
		
		NSMutableString text = new NSMutableString();
		text.appendString(NSTR("\n\nhello world is \nso simple !\n\n")); 
		
		textView.setText(text);
		
		mWindow.addSubview(textView);
	}
	
	public void test3() {
		mTextField = new UITextField(CGRectMake(20, 20, 320, 60)); 
		mTextField.setBorderStyle(UITextField.UITextBorderStyleRoundedRect);
		mTextField.setClearButtonMode(UITextField.UITextFieldViewModeWhileEditing);
		//mTextField.setKeyboardType(UIKeyInput.UIKeyboardTypeNumberPad);
		mTextField.setPlaceholder(NSTR("say something...")); 
		mTextField.setClearsOnBeginEditing(true);
		
		//mTextField.setText(NSTR("Hello World")); 
		//mTextField.setSecureTextEntry(true);
		mWindow.addSubview(mTextField);
	}
	
	public void test() {
		CGRect bounds = UIScreen.mainScreen().applicationFrame();
		UIView view = new MyView(bounds);
		mWindow.addSubview(view); 
		
		UIGraphicsBeginImageContext(CGSizeMake(14, 10));
		CGContext ctx = UIGraphicsGetCurrentContext();
		CGContextSetFillColor(ctx, new float[]{197/255f,204/255f,212/255f}); 
		CGContextFillRect(ctx, CGRectMake(0, 0, 14, 10));
		CGContextSetFillColor(ctx, new float[]{203/255f,210/255f,216/255f}); 
		CGContextFillRect(ctx, CGRectMake(0, 0, 4, 10));
		UIImage image = UIGraphicsGetImageFromCurrentImageContext();
		UIGraphicsEndImageContext();
		
		UIColor color = new UIColor(image);
		view.setBackgroundColor(color);
	}
	
	public void test2() {
		UIImageView top = new UIImageView(CGRectMake(15, 15, 450, 80)); 
		top.setContentStretch(CGRectMake(0.5f, 0.7f, 0.1f, 0.1f)); 
		top.setImage(UIImage.imageNamed(NSTR("cocoa/table_group_cell_background_top")));  
		
		UIImageView middle = new UIImageView(CGRectMake(15, 95, 450, 80)); 
		middle.setContentStretch(CGRectMake(0.5f, 0.5f, 0.1f, 0.1f)); 
		middle.setImage(UIImage.imageNamed(NSTR("cocoa/table_group_cell_background_middle"))); 
		
		UIImageView bottom = new UIImageView(CGRectMake(15, 175, 450, 80)); 
		bottom.setContentStretch(CGRectMake(0.5f, 0.2f, 0.1f, 0.1f)); 
		bottom.setImage(UIImage.imageNamed(NSTR("cocoa/table_group_cell_background_bottom"))); 
		
		mWindow.addSubview(top); 
		mWindow.addSubview(middle);
		mWindow.addSubview(bottom);
		
	} 
	 
	
	
	@Override
	public void applicationDidReceiveMenuCommand(int cmd, String title) {
		if(cmd == 1) {
			
			dialog = new UIAlertView();
			dialog.setTitle(NSTR("Version 4.2"));
			dialog.setMessage(NSTR("That is a tough dicision !"));
			dialog.addButtonWithTitle(NSTR("Yes")); 
			dialog.addButtonWithTitle(NSTR("No"));
			dialog.addButtonWithTitle(NSTR("Quit"));
			
			dialog.show();
			  
		} else if(cmd == 2) {
			
			dialog.dismissWithClickedButtonIndexAnimated(0, true);
			
		} else if(cmd == 3) {
			
			
			
		} else {
			
			UIView.animateWithDurationAnimations(2, new UIAnimationBlock() {
				@Override
				public void run() {
					CGPoint center = mView.center();
					center.y += 200;
					mView.setCenter(center);
				}
			});
			
		}
	}

	@Override
	public void applicationWillEnterForeground(UIApplication application) {

	}

	@Override
	public void applicationDidEnterBackground(UIApplication applicaiton) {

	}

	@Override
	public void applicationWillTerminate(UIApplication application) {

	}

	@Override
	public UIView viewForZoomingInScrollView(UIScrollView scrollView) {
		return mImageView;
	}

	@Override
	public void scrollViewDidScroll(UIScrollView scrollView) {
		//NSLog("scroll view did scroll: " + scrollView.contentOffset());
	}

	@Override
	public void scrollViewDidEndDecelerating(UIScrollView scrollView) {
		NSLog("scroll view did end decelerate !");
	}

	@Override
	public void scrollViewDidEndDraggingWillDecelerate(UIScrollView scrollView,
			boolean decelerate) {
		NSLog("scroll view did end dragging: " + decelerate);
	}

	@Override
	public void scrollViewWillBeginDecelerating(UIScrollView scrollView) {
		NSLog("scroll view will begin decelerate !");
	}

	@Override
	public void pickerViewDidSelectRowInComponent(UIPickerView pickerView,
			int row, int component) {
		NSLog("picker did select: %d %d", row, component);
	}

	@Override
	public NSString pickerViewTitleForRowForComponent(UIPickerView pickerView,
			int row, int component) {
		return NSTR("wheel " + (row + 1) + " " + (component + 1));
	}

	@Override
	public int numberOfComponentsInPickerView(UIPickerView pickerView) {
		return 2;
	}

	@Override
	public int pickerViewNumberOfRowsInComponent(UIPickerView pickerView,
			int component) {
		return 10;
	}


}
